home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtobbn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  3.1 KB  |  153 lines

  1. /* pbmtobg.c - read a portable bitmap and produce BitGraph graphics
  2. **
  3. ** Copyright 1989 by Mike Parker.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.   
  13. /*
  14. ** Changed to take advantage of negative Packed Pixed Data values and
  15. ** supply ANSI-standard string terminator.  Paul Milazzo, 28 May 1990.
  16. */
  17.  
  18. #include "pbm.h"
  19.  
  20. static void write16 ARGS(( unsigned int    ));
  21.  
  22. static int nco;
  23.  
  24. void
  25. main(argc,argv)
  26. int argc;
  27. char **argv;
  28. {
  29.  int rows;
  30.  int cols;
  31.  int format;
  32.  bit *bitrow;
  33.  int row;
  34.  unsigned int sixteen;
  35.  int i;
  36.  unsigned int mask;
  37.  int op;
  38.  int x;
  39.  int y;
  40.  
  41.  pbm_init( &argc, argv );
  42.  
  43.  op = 3;
  44.  switch (argc)
  45.   { case 1:
  46.        break;
  47.     case 2:
  48.        op = atoi(argv[1]);
  49.        break;
  50.     case 3:
  51.        x = atoi(argv[1]);
  52.        y = atoi(argv[2]);
  53.        fprintf (stdout, "\33:%d;%dm",x,y);
  54.        break;
  55.     case 4:
  56.        op = atoi(argv[1]);
  57.        x = atoi(argv[2]);
  58.        y = atoi(argv[3]);
  59.        fprintf (stdout, "\33:%d;%dm",x,y);
  60.        break;
  61.   }
  62.  nco = 0;
  63.  pbm_readpbminit(stdin,&cols,&rows,&format);
  64.  fprintf (stdout, "\33P:%d;%d;%ds\n",op,cols,rows);
  65.  bitrow = pbm_allocrow(cols);
  66.  for (row=0;row<rows;row++)
  67.   { pbm_readpbmrow(stdin,bitrow,cols,format);
  68.     sixteen = 0;
  69.     mask = 0x8000;
  70.     for (i=0;i<cols;i++)
  71.      { if (bitrow[i]==PBM_BLACK) sixteen |= mask;
  72.        mask >>= 1;
  73.        if (mask == 0)
  74.     { mask = 0x8000;
  75.       write16(sixteen);
  76.       sixteen = 0;
  77.     }
  78.      }
  79.     if (mask != 0x8000)
  80.      { write16(sixteen);
  81.      }
  82.   }
  83.  puts("\033\\");
  84.     pm_close (stdout);
  85.  exit(0);
  86. }
  87.  
  88. #ifdef POSITIVE_VALUES_ONLY
  89. static void
  90. write16(sixteen)
  91. unsigned int sixteen;
  92. {
  93.  if (nco > 75)
  94.   { putchar('\n');
  95.     nco = 0;
  96.   }
  97.  if (sixteen & 0xfc00)
  98.   { putchar(0100+(sixteen>>10));
  99.     nco ++;
  100.   }
  101.  if (sixteen & 0xfff0)
  102.   { putchar(0100+((sixteen>>4)&0x3f));
  103.     nco ++;
  104.   }
  105.  putchar(060+(sixteen&0xf));
  106.  nco ++;
  107. }
  108. #else
  109. /*
  110.  *  This version of "write16" uses negative Packed Pixel Data values to
  111.  *  represent numbers in the range 0x7fff--0xffff; negative values will
  112.  *  require fewer characters as they approach the upper end of that range.
  113.  */
  114. static void
  115. write16 (word)
  116. unsigned int    word;
  117. {
  118.     int        high;
  119.     int        mid;
  120.     int        low;
  121.     int        signChar;
  122.  
  123.     if (nco > 75) {
  124.     putchar ('\n');
  125.     nco = 0;
  126.     }
  127.  
  128.     if (word > 0x7fff) {
  129.     word = (unsigned int) (0x10000L - (long) word);
  130.     signChar = ' ';
  131.     }
  132.     else
  133.     signChar = '0';
  134.  
  135.     high = (word >> 10) + '@';
  136.     mid    = ((word & 0x3f0) >> 4) + '@';
  137.     low    = (word & 0xf) + signChar;
  138.  
  139.     if (high != '@') {
  140.     fprintf (stdout, "%c%c%c", high, mid, low);
  141.     nco += 3;
  142.     }
  143.     else if (mid != '@') {
  144.     fprintf (stdout, "%c%c", mid, low);
  145.     nco += 2;
  146.     }
  147.     else {
  148.     putchar (low);
  149.     nco++;
  150.     }
  151. }
  152. #endif
  153.